Imports DAL
Imports System.Configuration
Imports MyClasses
Public Class frmProductTransaction
#Region "Declaration"
    Dim btLoadFlag As Byte
#End Region

#Region "Form Control Events"

    Private Sub frmProductTransaction_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try
            'Dim dt As DateTime = Now
            btLoadFlag = 1
            Call BCommon.SetFormStandards(Me) 'Kiran@ITS on 20071205:Call for Form Setting.        
            Call DisplayData()
            Call SetcontrolTag()
            btLoadFlag = 0
            'MessageBox.Show((Now - dt).ToString)
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "frmProductTransaction_Load")
        End Try
    End Sub

#End Region

#Region "Grid Events"
    Sub setcaption()
        Try
            With fgTrasSheet
                Dim intGridWidth As Integer = .Width - 5
                .Cols.Count = 7
                .Cols(0).Visible = False
                .Cols(1).Caption = "Date"
                .Cols(1).Format = "dd/MM/yy"
                .Cols(1).Width = intGridWidth / 8.2
                .Cols(2).Caption = "Description"
                .Cols(2).Width = intGridWidth / 2.8
                .Cols(3).Caption = "In Quantity"
                .Cols(3).Width = intGridWidth / 8.2
                .Cols(4).Caption = "Out Quantity"
                .Cols(4).Width = intGridWidth / 8.2
                .Cols(5).Caption = "In Value"
                .Cols(5).Width = intGridWidth / 8
                .Cols(6).Caption = "Out Value"
                .Cols(6).Width = intGridWidth / 8
            End With
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "setcaption")
        End Try

    End Sub
#End Region

#Region "User Defined Funcitons"

    Private Sub DisplayData()
        Try
            btLoadFlag = 1
            Call ClearData()
            BCommon.SetComboValues(cmbCompany, DManufacturer.GetManufacturers(), "mfctrID", "mfctrName")
            BCommon.SetComboValues(cmbVendor, BVendor.GetVendor(), "VendorID", "VendorName")
            BCommon.SetComboValues(cmbProduct, BSaleReturn.GetItem(0, False), "ItemID", "ItemName")
            BCommon.SetComboValues(cmbBatch, DItem.GetItemsBatch(cmbProduct.SelectedValue, True), "BatchNo", "BatchNo")
            'BtnSearch.Enabled = False
            Call FillData()

            btLoadFlag = 0
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "DisplayData")
        End Try
    End Sub
    Private Sub ClearData()
        Try
            fgTrasSheet.DataSource = Nothing
            fgTrasSheet.Clear(C1.Win.C1FlexGrid.ClearFlags.Content, 1, 0, fgTrasSheet.Rows.Count - 1, fgTrasSheet.Cols.Count - 1)
            cmbProduct.SelectedIndex = -1
            cmbBatch.SelectedIndex = -1
            cmbCompany.SelectedIndex = -1
            cmbVendor.SelectedIndex = -1
            txtVendorID.Text = ""
            If System.Configuration.ConfigurationManager.AppSettings("PrdTransDateRange") = "Month" Then              'Month
                dtpFrmDt.Value = Now.Date().AddMonths(-1)
            ElseIf System.Configuration.ConfigurationManager.AppSettings("PrdTransDateRange") = "Week" Then           'Week
                dtpFrmDt.Value = Now.Date().AddDays(-7)
            Else
                dtpFrmDt.Value = Now.Date                                                                           'Day
            End If
            'dtpFrmDt.Value = Date.Now()
            dtpToDt.Value = Now.Date
            cmbOperation.SelectedIndex = -1

            If ConfigurationManager.AppSettings("FormMode") = "R" Then
                cmbProduct.Enabled = True
                cmbCompany.Enabled = True
                dtpFrmDt.Enabled = True
                cmbBatch.Enabled = True
                txtVendorID.Enabled = True
                cmbVendor.Enabled = True
                dtpToDt.Enabled = True
                cmbOperation.Enabled = True
            End If

        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "ClearData")
        End Try
    End Sub

    Private Sub FillData()
        Try
            Dim dtTrans As DataTable
            Dim objProdTrans As CProdTrans = SetData()
            Dim intcount As Integer = 1
            fgTrasSheet.DataSource = Nothing
            fgTrasSheet.Clear(C1.Win.C1FlexGrid.ClearFlags.Content, 1, 0, fgTrasSheet.Rows.Count - 1, fgTrasSheet.Cols.Count - 1)
            With fgTrasSheet
                dtTrans = DProdTrans.GetTrans(objProdTrans)
                If BtnSearch.Tag = "Search" AndAlso dtTrans.Rows.Count <= 0 Then
                    BCommon.ShowMsgError(10343)
                    BtnSearch.Tag = String.Empty
                End If

                If dtTrans.Rows.Count > 18 Then
                    fgTrasSheet.Rows.Count = dtTrans.Rows.Count + 1
                ElseIf dtTrans.Rows.Count = 18 Then
                    fgTrasSheet.Rows.Count = 19
                Else
                    fgTrasSheet.Rows.Count = 18
                End If

                For Each row As DataRow In dtTrans.Rows
                    fgTrasSheet.Item(intcount, 1) = row.Item(0)                      'Date
                    fgTrasSheet.Item(intcount, 2) = row.Item(1)                      'Description
                    fgTrasSheet.Item(intcount, 3) = row.Item(2)                      'Inquantity
                    fgTrasSheet.Item(intcount, 4) = row.Item(3)                      'OutQuantity
                    fgTrasSheet.Item(intcount, 5) = row.Item(4)                      'InValue
                    fgTrasSheet.Item(intcount, 6) = row.Item(5)                      'OutValue
                    intcount = intcount + 1
                Next

                Call setcaption()
            End With
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "FillData")
        End Try
    End Sub

    Private Function SetData() As CProdTrans
        Dim objProdTrans As New CProdTrans
        Try
            With objProdTrans
                .Company = cmbCompany.SelectedValue()
                .Vendor = cmbVendor.SelectedValue()
                .Product = cmbProduct.SelectedValue()
                '.Batch = cmbBatch.SelectedText()
                .Batch = cmbBatch.SelectedValue()
                .FromDate = dtpFrmDt.Value.Date()
                .ToDate = dtpToDt.Value.Date()
                .Operation = cmbOperation.SelectedIndex()
            End With
            Return objProdTrans
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "SetData")
            Return Nothing
        End Try
    End Function
    '**********************************************
    'Modified BY:-VINIT(2008/02/16)
    'Set the tag of the control for data validation
    '==============================
    Private Sub SetcontrolTag()
        cmbProduct.Tag = cmbProduct.Text
        cmbBatch.Tag = cmbBatch.Text
        cmbCompany.Tag = cmbCompany.Text
        cmbVendor.Tag = cmbVendor.Text
        txtVendorID.Tag = txtVendorID.Text
    End Sub

#End Region

#Region "Key Down Events"
    Private Sub frmProductTransaction_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp
        If e.KeyCode = Keys.F1 Then
            'Rauhl_ITS@20073012 : Add following code display help
            e.SuppressKeyPress = True
            Help.ShowHelp(Me, "Pharma.chm", HelpNavigator.KeywordIndex, "Product Transaction Sheet")
        End If
    End Sub 'VARIABLE IS USED TO AVOID FUNCTIONS WHILE LOADING THE DATA  1: LOADING THE DATA EXIT SUB  0: DATA IS LOADED

    Private Sub cmbProduct_DropDownClosed(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmbProduct.DropDownClosed
        If btLoadFlag = 1 Then Exit Sub
        If cmbProduct.SelectedValue <> Nothing Then
            If cmbProduct.SelectedIndex >= 0 Or Not IsNothing(cmbProduct.SelectedIndex) Then
                If DItem.GetItemsBatch(cmbProduct.SelectedValue, True).Rows.Count > 0 Then
                    BCommon.SetComboValues(cmbBatch, DItem.GetItemsBatch(cmbProduct.SelectedValue, True), "BatchNo", "BatchNo")
                Else
                    cmbBatch.DataSource = Nothing
                End If
            Else
                BCommon.SetComboValues(cmbBatch, DItem.GetItemsBatch(0, True), "BatchNo", "BatchNo")
            End If
        Else
            cmbBatch.DataSource = Nothing
        End If

        Call FillData()
    End Sub

    Private Sub cmbProduct_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles cmbProduct.KeyDown
        If e.KeyCode = Keys.Enter Then
            FillData()
            cmbBatch.Focus()
        End If
    End Sub
    Private Sub cmbBatch_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles cmbBatch.KeyDown
        If e.KeyCode = Keys.Enter Then
            FillData()
            cmbCompany.Focus()
        End If
    End Sub

    Private Sub cmbCompany_DropDownClosed(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmbCompany.DropDownClosed
        If btLoadFlag = 1 Then Exit Sub
        If cmbCompany.SelectedIndex <> Nothing Then

            If BCommon.GetVendor(cmbCompany.SelectedValue).Rows.Count > 0 Then
                BCommon.SetComboValues(cmbVendor, BCommon.GetVendor(cmbCompany.SelectedValue), "VendorID", "Name")
                If cmbVendor.SelectedIndex <= 0 Then
                    txtVendorID.Text = ""
                Else
                    txtVendorID.Text = ""
                    txtVendorID.Text = BCommon.GetSequentialCode("VENDOR") & Trim(cmbVendor.SelectedValue.ToString)
                End If

            Else
                cmbVendor.DataSource = Nothing
            End If
            If DProdTrans.GetItems(cmbCompany.SelectedValue).Rows.Count > 0 Then
                BCommon.SetComboValues(cmbProduct, DProdTrans.GetItems(cmbCompany.SelectedValue), "ItemID", "ItemName")
            Else
                cmbProduct.DataSource = Nothing
            End If
            If Not IsNothing(cmbProduct.SelectedValue) Then
                If DItem.GetItemsBatch(cmbProduct.SelectedValue, True).Rows.Count > 0 Then
                    BCommon.SetComboValues(cmbBatch, DItem.GetItemsBatch(cmbProduct.SelectedValue, True), "BatchNo", "BatchNo")
                Else
                    cmbBatch.DataSource = Nothing
                End If
            Else
                cmbBatch.DataSource = Nothing
            End If
        Else
            BCommon.SetComboValues(cmbVendor, BVendor.GetVendor(), "VendorID", "VendorName")
            BCommon.SetComboValues(cmbProduct, BSaleReturn.GetItem(0, False), "ItemID", "ItemName")
            BCommon.SetComboValues(cmbBatch, DItem.GetItemsBatch(cmbProduct.SelectedValue, True), "BatchNo", "BatchNo")
        End If
        Call FillData()
    End Sub

    Private Sub cmbCompany_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles cmbCompany.KeyDown
        If e.KeyCode = Keys.Enter Then
            FillData()
            txtVendorID.Focus()
        End If
    End Sub

    Private Sub cmbVendor_DropDownClosed(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmbVendor.DropDownClosed
        Try
            If btLoadFlag = 1 Then Exit Sub
            If cmbVendor.SelectedIndex <> Nothing Then
                txtVendorID.Text = ""
                txtVendorID.Text = BCommon.GetSequentialCode("VENDOR") & Trim(cmbVendor.SelectedValue.ToString)
                If cmbCompany.SelectedIndex <> Nothing Then
                    If DItem.GetvendorItems(cmbCompany.SelectedValue, cmbVendor.SelectedValue).Rows.Count > 0 Then
                        BCommon.SetComboValues(cmbProduct, DItem.GetvendorItems(cmbCompany.SelectedValue, cmbVendor.SelectedValue), "ItemID", "ItemName")
                    Else : cmbProduct.DataSource = Nothing
                    End If
                    If Not IsNothing(cmbProduct.SelectedValue) Then
                        If DItem.GetItemsBatch(cmbProduct.SelectedValue, True).Rows.Count > 0 Then
                            BCommon.SetComboValues(cmbBatch, DItem.GetItemsBatch(cmbProduct.SelectedValue, True), "BatchNo", "BatchNo")
                        Else : cmbBatch.DataSource = Nothing
                        End If
                    Else : cmbBatch.DataSource = Nothing
                    End If
                Else
                    If DProdTrans.GetItem(cmbVendor.SelectedValue).Rows.Count > 0 Then
                        BCommon.SetComboValues(cmbProduct, DProdTrans.GetItem(cmbVendor.SelectedValue), "ItemID", "ItemName")
                    Else : cmbProduct.DataSource = Nothing
                    End If
                    If DItem.GetItemsBatch(cmbProduct.SelectedValue, True).Rows.Count > 0 Then
                        BCommon.SetComboValues(cmbBatch, DItem.GetItemsBatch(cmbProduct.SelectedValue, True), "BatchNo", "BatchNo")
                    Else : cmbBatch.DataSource = Nothing
                    End If
                End If
            Else
                txtVendorID.Text = ""
                If cmbCompany.SelectedIndex <> Nothing Then
                    If DProdTrans.GetItems(cmbCompany.SelectedValue).Rows.Count > 0 Then
                        BCommon.SetComboValues(cmbProduct, DProdTrans.GetItems(cmbCompany.SelectedValue), "ItemID", "ItemName")
                    Else : cmbProduct.DataSource = Nothing
                    End If
                    If DItem.GetItemsBatch(cmbProduct.SelectedValue, True).Rows.Count > 0 Then
                        BCommon.SetComboValues(cmbBatch, DItem.GetItemsBatch(cmbProduct.SelectedValue, True), "BatchNo", "BatchNo")
                    Else : cmbBatch.DataSource = Nothing
                    End If
                Else : BCommon.SetComboValues(cmbProduct, BSaleReturn.GetItem(0, False), "ItemID", "ItemName")
                End If
            End If
            Call FillData()
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "cmbVendor_DropDownClosed")
        End Try
    End Sub

    Private Sub cmbVendor_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles cmbVendor.KeyDown
        If e.KeyCode = Keys.Enter Then
            If cmbVendor.SelectedIndex > 0 Then
                Me.txtVendorID.Text = BCommon.GetSequentialCode("VENDOR") & Trim(Me.cmbVendor.SelectedValue.ToString())
            Else : Me.txtVendorID.Text = ""
            End If
            FillData()
            dtpFrmDt.Focus()
        End If
    End Sub

    Private Sub dtpFrmDt_CloseUp(ByVal sender As Object, ByVal e As System.EventArgs) Handles dtpFrmDt.CloseUp
        If dtpFrmDt.Value.Date > Now.Date Then
            BCommon.ShowMsgError(10292)
            dtpFrmDt.Value = Format(Now.Date, "MMM/yy")
            dtpFrmDt.Focus()
            Exit Sub
        ElseIf dtpToDt.Value.Date > Now.Date Then
            BCommon.ShowMsgError(10293)
            dtpToDt.Value = Format(Now.Date, "MMM/yy")
            dtpToDt.Focus()
            Exit Sub
        ElseIf dtpFrmDt.Value.Date > dtpToDt.Value.Date Then
            BCommon.ShowMsgError(10244)
            dtpFrmDt.Value = Format(Now.Date, "MMM/yy")
            dtpFrmDt.Focus()
            Exit Sub
        End If
        FillData()
    End Sub

    Private Sub dtpFrmDt_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles dtpFrmDt.KeyDown
        If e.KeyCode = Keys.Enter Then
            If dtpFrmDt.Value.Date > Now.Date Then
                BCommon.ShowMsgError(10292)
                dtpFrmDt.Value = Format(Now.Date, "MMM/yy")
                dtpFrmDt.Focus()
                Exit Sub
            ElseIf dtpToDt.Value.Date > Now.Date Then
                BCommon.ShowMsgError(10293)
                dtpToDt.Value = Format(Now.Date, "MMM/yy")
                dtpToDt.Focus()
                Exit Sub
            ElseIf dtpFrmDt.Value.Date > dtpToDt.Value.Date Then
                BCommon.ShowMsgError(10244)
                dtpFrmDt.Value = Format(Now.Date, "MMM/yy")
                dtpFrmDt.Focus()
                Exit Sub
            End If
            FillData()
            dtpToDt.Focus()
        End If
    End Sub

    Private Sub dtpToDt_CloseUp(ByVal sender As Object, ByVal e As System.EventArgs) Handles dtpToDt.CloseUp
        If dtpFrmDt.Value.Date > Now.Date Then
            BCommon.ShowMsgError(10292)
            dtpFrmDt.Value = Format(Now.Date, "MMM/yy")
            dtpFrmDt.Focus()
            Exit Sub
        ElseIf dtpToDt.Value.Date > Now.Date Then
            BCommon.ShowMsgError(10293)
            dtpToDt.Value = Format(Now.Date, "MMM/yy")
            dtpToDt.Focus()
            Exit Sub
        ElseIf dtpFrmDt.Value.Date > dtpToDt.Value.Date Then
            BCommon.ShowMsgError(10244)
            dtpFrmDt.Value = Format(Now.Date, "MMM/yy")
            dtpFrmDt.Focus()
            Exit Sub
        End If
        FillData()
    End Sub

    Private Sub dtpToDt_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles dtpToDt.KeyDown
        If e.KeyCode = Keys.Enter Then
            If dtpFrmDt.Value.Date > Now.Date Then
                BCommon.ShowMsgError(10292)
                dtpFrmDt.Value = Format(Now.Date, "MMM/yy")
                dtpFrmDt.Focus()
                Exit Sub
            ElseIf dtpToDt.Value.Date > Now.Date Then
                BCommon.ShowMsgError(10293)
                dtpToDt.Value = Format(Now.Date, "MMM/yy")
                dtpToDt.Focus()
                Exit Sub
            ElseIf dtpFrmDt.Value.Date > dtpToDt.Value.Date Then
                BCommon.ShowMsgError(10244)
                dtpFrmDt.Value = Format(Now.Date, "MMM/yy")
                dtpFrmDt.Focus()
                Exit Sub
            End If
            FillData()
            cmbOperation.Focus()
        End If
    End Sub

    Private Sub cmbOperation_DropDownClosed(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmbOperation.DropDownClosed
        If btLoadFlag = 1 Then Exit Sub
        Call FillData()
    End Sub

    Private Sub cmbOperation_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles cmbOperation.KeyDown
        If e.KeyCode = Keys.Enter Then
            FillData()
            BtnSearch.Focus()
        End If
    End Sub

    Private Sub txtVendorID_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtVendorID.KeyDown
        If e.KeyCode = Keys.Enter Then SendKeys.Send("{TAB}")
    End Sub

    Private Sub txtVendorID_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtVendorID.LostFocus
        Dim intVendId As Integer
        Dim dsVendorId As New DataSet
        Try
            If Me.txtVendorID.Text <> "" Then
                dsVendorId = BVendor.GetVendorId(txtVendorID.Text.ToUpper())
                If dsVendorId.Tables(0).Rows.Count > 0 Then
                    intVendId = Convert.ToInt32(dsVendorId.Tables(0).Rows(0).Item(0).ToString())
                    cmbVendor.SelectedValue = intVendId
                    cmbVendor.Focus()
                Else
                    cmbVendor.SelectedValue = 0
                    'MessageBox.Show(BCommon.GetMessage(10277), "Pharmacy Message", MessageBoxButtons.OK, MessageBoxIcon.Information)
                    BCommon.ShowMsgError(10277)
                    txtVendorID.Text = ""
                    Me.txtVendorID.Focus()
                End If
            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "txtVendorID_LostFocus")
        End Try
    End Sub

    Private Sub cmbBatch_SelectedValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmbBatch.SelectedValueChanged
        If btLoadFlag = 1 Then Exit Sub
        Call FillData()
    End Sub

#End Region

#Region "Button Click Events"
    Private Sub BtnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnSearch.Click
        BtnSearch.Tag = "Search"
        FillData()
    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Call DisplayData()
    End Sub

    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Me.Close()
    End Sub
#End Region

End Class